<template>
	<view>
		<view class="tags-box">
			<view @click="tag = i.id" v-for="(i,index) in tags" class="tag" :style="{backgroundColor:(i.id == tag ? '#FEBF4E' : '#FFF')}">{{i.name}}</view>
		</view>
		<view class="info-box">
			<view class="info-each">
				<view class="">附近商圈</view>
				<view class="input">
					<input v-model="village" type="text" placeholder="请输入(非必填)" />
				</view>
			</view>
		</view>

		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">标题</view>
				<view class="input">
					<input v-model="title" type="text" placeholder="请输入" />
				</view>
			</view>
			<view class="wire"></view>
			<view class="info-each">
				<view class="">面积</view>
				<view class="input">
					<input v-model="area" type="number" placeholder="请输入" />
					<text>㎡</text>
				</view>
			</view>
			<view class="wire"></view>
			<view class="info-each">
				<view class="">租金</view>
				<view class="input">
					<input v-model="price" type="number" placeholder="请输入" />
					<text>元/月</text>
				</view>
			</view>
			
			<view class="wire"></view>
			<view class="tags-box">
				<view class="">房源配套</view>
				<view class="tags">
					<view class="tag" v-for="(i,index) in facilities">
						<input v-model="facilities[index]" type="text" placeholder="请输入">
						<image src="../../static/del-tag.png" @click="del(index)" mode=""></image>
					</view>
					<view class="tag" style="color: #999999" @click="addTag">
						添加
						<image src="../../static/add-tag.png" mode=""></image>
					</view>
				</view>
			</view>
			<view class="wire"></view>
			<view class="introduce-box">
				<view class="">房源介绍</view>
				<view class="textarea-box">
					<textarea v-model="introduce" name="" id="" cols="30" rows="10" placeholder="请输入"></textarea>
				</view>
			</view>
		</view>

		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">联系人</view>
				<view class="input">
					<input v-model="name" type="text" placeholder="请输入" />
				</view>
			</view>
			<view class="wire"></view>
			<view class="info-each">
				<view class="">电话</view>
				<view class="input">
					<input v-model="mobile" type="number" placeholder="请输入" />
				</view>
			</view>
		</view>

		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">主图</view>
				<view class="">
					<text class="tint">请上传主图</text>
				</view>
			</view>
			<view class="upimg">
				<u-upload width="65" height="65" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic"
					name="1" multiple :maxCount="9">
					<image src="../../static/imgs.png" mode="widthFix"
						style="width: 130rpx;height: 130rpx;">
					</image>
				</u-upload>
			</view>
		</view>
		
		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">展示视频</view>
				<view class="">
					<!-- <text class="tint">请上传展示视频</text> -->
				</view>
			</view>
			<view style="color: #999999;font-size: 22rpx;padding-top: 5rpx;">视频不要求必须上传，视频上传后，将会在平台上展示，同时半柿团队会将视频发到视频平台上帮你免费宣传</view>
			<view class="upimg">
				<u-upload
					:fileList="fileList2"
					@afterRead="afterRead"
					@delete="deletePic"
					name="2"
					multiple
					:maxCount="1"
					accept="video"
				>
				<image src="../../static/imgs.png" mode="widthFix"
					style="width: 130rpx;height: 130rpx;">
				</image>
				</u-upload>
			</view>
		</view>
		
		<view class="info-box" style="margin-top: 24rpx">
			<view class="info-each">
				<view class="">视频库视频</view>
				<view class="">
					<!-- <text class="tint">请上传视频库视频</text> -->
				</view>
			</view>
			<view style="color: #999999;font-size: 22rpx;padding-top: 5rpx;">视视频库的视频，主要用于视频平台的宣传，一个视频只能发布一次，为了更好的宣传效果，你可以将同一个信息的视频，从不同的位置拍摄多个视频上传，由半柿平台免费帮你多次宣传，快速促进您信息的成交</view>
			<view class="upimg">
				<u-upload
					:fileList="fileList3"
					@afterRead="afterRead"
					@delete="deletePic"
					name="3"
					multiple
					:maxCount="6"
					accept="video"
				>
				<image src="../../static/imgs.png" mode="widthFix"
					style="width: 130rpx;height: 130rpx;">
				</image>
				</u-upload>
			</view>
		</view>
		
		<view class="cotton"></view>
		<view class="btn-box">
			<view class="btn" @click="issue" v-if="id">修改</view>
			<view class="btn" @click="issue" v-else>发布</view>
		</view>
		
		
		<TopPopup :show1="show1" :meal="meal" @padlock="padlock" @ckeck="ckeck"></TopPopup>
		
		<view class="issue" @click="toVideo">
			拍摄教程
		</view>
		
		<popupPay @padlock="padlock" :order_id='order_id' :type='1' :showPay='showPay' :money="money"></popupPay>
	</view>
</template>

<script>
	import TopPopup from "@/components/topPopup.vue"
	import popupPay from '@/components/popup-pay.vue'
export default {
	components: { TopPopup,popupPay },
	data() {
		return {
			fileList1: [],
			fileList2:[],//视频 单
			fileList3:[],//视频 多
			tags:[],
			tag:1,
			village:'',
			title:'',
			area:'',
			price:'',
			introduce:'',
			facilities:[''],//房源配套
			images:'',
			name:'',
			mobile:'',
			id:'',
			show1: false,
			config:uni.getStorageSync('config'),
			
			order_id:'',
			itemid:'',
			meal: [],
			current:null,
			
			myMoney:'',
			paytype:null,
			
			showPay:false,
			money:''
		};
	},
	onLoad(op) {
		if(op.id){
			this.id = op.id
			this.gethouseDetail(op.id)
			uni.setNavigationBarTitle({
			  title:'商业地产编辑'
			});
		}
		this.getTags()
		this.getcombo()
		this.getMoney()
		// #ifdef MP-WEIXIN
		this.paytype = 2
		// #endif
		// #ifdef APP
		this.paytype = 1
		// #endif
		this.noticeorderconfig()
	},
	methods: {
		noticeorderconfig(){
			uni.$u.http.post('/noticeorderconfig').then(res => {
				if (res.code === 1) {
					this.money = res.data.trans_price
				}
			}).catch((res) => {
				
			})
		},
		toVideo(){
			uni.navigateTo({
				url:'/pages/videoShopping/videoShopping?types=2'
			})
		},
		padlock(){
			this.show1 = false
			this.showPay = false
			uni.navigateBack()
		},
		getMoney(){
			uni.$u.http.post('/getmoney ').then(res => {
				if (res.code === 1) {
					this.myMoney = res.data
				}
			}).catch((res) => {
				
			})
		},
		ckeck(e) {
			if(!e.id) return this.toast('请选择置顶套餐')
			if(this.myMoney < e.money){
				//余额不足 微信支付
				uni.$u.http.post('/meallist', {
					type:3,
					order_id: this.order_id,
					id:e.id,
					paytype:this.paytype
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						let that = this
						uni.requestPayment({
							appId: res.data.pay.appId,
							nonceStr: res.data.pay.nonceStr,
							package: res.data.pay.package,
							paySign: res.data.pay.paySign,
							signType: res.data.pay.signType,
							timeStamp: res.data.pay.timeStamp,
							success: (e) => {
								that.toast(res.msg)
								that.show1 = false
								setTimeout(() => {
									uni.navigateBack()
								},500)
							},
							fail: (e) => {
								that.$u.toast('支付失败');
							}
						})
					}
				}).catch((res) => {
						
				})
			}else {
				//余额支付
				uni.$u.http.post('/meallist', {
					type:3,
					order_id: this.order_id,
					id:e.id,
					paytype:4
				}).then(res => {
					console.log(res)
					if (res.code == 1) {
						this.toast(res.msg)
						this.show1 = false
						setTimeout(() => {
							uni.navigateBack()
						},500)
					}
				}).catch((res) => {
						
				})
			}
		},
		getcombo() {
			uni.$u.http.post('/meallist',{
				mealtype:3
			}).then(res => {
				if (res.code == 1) {
					this.meal = res.data
				}
			}).catch((res) => {
			})
		},
		toidle(){
			uni.navigateBack()
		},
		open() {
			// console.log('open');
		},
		close() {
			this.show1 = false;
			// console.log('close');
		},
		gethouseDetail(id){
			uni.$u.http.post('/transference/editInfo',{
				id
			}).then(res => {
				if (res.code == 1) {
					this.village = res.data.village
					this.title = res.data.title
					this.area = res.data.area
					this.price = res.data.price
					this.introduce = res.data.introduce
					this.name = res.data.name
					this.facilities = res.data.facilities.split(",")
					this.mobile = res.data.mobile
					this.tag = res.data.transcate_id
					this.fileList1 = res.data.images.split(",").map(value => {
						return {
							url: this.oss(value),
							ful: value
						}
					})
					if(res.data.video){
						this.fileList2 = res.data.video.split(",").map(value => {
							return {
								url: this.oss(value),
								ful: value
							}
						})
					}
					if(res.data.videos){
						this.fileList3 = res.data.videos.split(",").map(value => {
							return {
								url: this.oss(value),
								ful: value
							}
						})
					}
				}
			}).catch((res) => {
			
			})
		},
		issue(){
			console.log(this.fileList1,'???')
			let images = [];
			this.fileList1.forEach(item => {
				images.push(item.url)
			})
			let video = [];
			this.fileList2.forEach(item => {
				video.push(item.url)
			})
			let videos = [];
			this.fileList3.forEach(item => {
				videos.push(item.url)
			})
			let data = {
				village:this.village,
				title:this.title,
				area:this.area,
				price:this.price,
				transcate_id:this.tag,
				facilities:this.facilities.join(','),
				introduce:this.introduce,
				images:images.join(','),
				name:this.name,
				mobile:this.mobile,
				type:this.id ? 'edit' : 'add',
				video:video.join(','),
				videos:videos.join(',')
			}
			if(this.id){
				data.id = this.id
			}
			console.log(data)
			uni.$u.http.post('/transference/postTransference',data).then(res => {
				if (res.code == 1) {
					if(this.id){
						uni.$u.toast(res.msg)
						setTimeout(() => {
							uni.navigateBack()
						},1000)
					} else {
						this.order_id = res.data
						
						if(Number(this.money) > 0){
							this.showPay = true
						}else {
							this.show1 = true
						}
					}
				}
			}).catch((res) => {
			
			})
		},
		addTag(){
			this.facilities.push('')
			console.log(this.facilities)
		},
		del(index){
			this.facilities.splice(index, 1);
		},
		getTags(){
			uni.$u.http.post('/transference/transcate').then(res => {
				if (res.code == 1) {
					this.tags = res.data
				}
			}).catch((res) => {
			
			})
		},
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)
				console.log(result)
				if (result) {
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url
					}))
					fileListLen++
				} else {
					this[`fileList${event.name}`].splice(fileListLen, 1)
				}
		
			}
			console.log(this[`fileList${event.name}`])
		},
		uploadFilePromise(url) {
			console.log(url)
			let that = this
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: that.Upload.uploadurl, // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					formData: {
						type: 'image',
						token: uni.getStorageSync('token')
					},
					success: (res) => {
						let data = JSON.parse(res.data)
						if (res.statusCode === 200 && data.code === 1 && data.data) {
							resolve(data.data)
						} else {
							resolve()
						}
					}
				});
			})
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f6f6f6;
}
.tags-box {
	padding: 32rpx;
	.tag {
		display: inline-block;
		font-size: 24rpx;
		font-weight: 500;
		color: #333333;
		padding: 20rpx 32rpx;
		background-color: #FFF;
		border-radius: 16rpx;
		margin-right: 16rpx;
		margin-top: 24rpx;
	}
}
.info-box {
	width: 622rpx;
	padding: 29rpx 32rpx;
	background: #ffffff;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	opacity: 1;
	margin: auto;

	.info-each {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;

		.input {
			display: flex;
			align-items: center;
			text-align: right;
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;
			text {
				display: inline-block;
				padding-left: 30rpx;
			}
		}

		image {
			width: 32rpx;
			height: 32rpx;
			position: relative;
			top: 7rpx;
			left: 20rpx;
		}
		.tint {
			font-size: 30rpx;
			font-weight: 500;
			color: #999999;
		}
	}
	.upimg {
		margin-top: 30rpx;
	}

	.wire {
		width: 622rpx;
		height: 1rpx;
		background-color: #f0f0f0;
		margin: auto;
		margin: 30rpx 0;
	}

	.tags-box {
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		.tags {
			display: flex;
			flex-wrap: wrap;
			// margin-top: 24rpx;
			.tag {
				background-color: #f6f6f6;
				font-size: 30rpx;
				padding: 19rpx 48rpx 19rpx 24rpx;
				font-weight: 500;
				color: #333333;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-right: 24rpx;
				display: flex;
				align-items: center;
				margin-top: 24rpx;
				image {
					width: 30rpx;
					height: 30rpx;
					position: relative;
					// top: 7rpx;
					left: 20rpx;
				}
				input {
					display: inline-block;
					width: 100rpx;
					transition: width 0.3s; /* 添加过渡效果 */
				}
			}
		}
	}

	.introduce-box {
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
		.textarea-box {
			padding: 24rpx 0;
			height: 180rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: #333333;
			textarea {
				width: 100%;
				height: 180rpx;
			}
		}
	}
}

.cotton {
	height: 160rpx;
}
.btn-box {
	width: 100%;
	position: fixed;
	bottom: 20rpx;
	z-index: 9;
	
	.btn {
		width: 686rpx;
		height: 98rpx;
		background: #FEBF4E;
		border-radius: 49rpx 49rpx 49rpx 49rpx;
		opacity: 1;
		margin: auto;
		text-align: center;
		line-height: 98rpx;
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
	}
}

.popup-box {
	width: 560rpx;
	// height: 914rpx;
	padding-bottom: 32rpx;
	background: #FFFFFF;
	border-radius: 24rpx 24rpx 24rpx 24rpx;
	opacity: 1;
	.pop-title {
		font-size: 32rpx;
		text-align: center;
		padding-top: 60rpx;
		font-weight: bold;
		color: #333333;
	}
	.pop-hint {
		font-size: 24rpx;
		text-align: center;
		padding-top: 16rpx;
		font-weight: 500;
		color: #999999;
	}
	.content {
		width: 432rpx;
		padding: 32rpx;
		// height: 264rpx;
		background: #F6F6F6;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		margin: auto;
		margin-top: 40rpx;
		margin-bottom: 24rpx;
		text {
			font-size: 28rpx;
			font-weight: 500;
			color: #666666;
			line-height: 50rpx;
		}
	}
	.mobile {
		font-size: 48rpx;
		font-weight: bold;
		color: #333333;
		text-align: center;
		padding-top: 16rpx;
	}
	.btn-1 {
		width: 496rpx;
		height: 80rpx;
		background: #FEBF4E;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		text-align: center;
		line-height: 80rpx;
		font-size: 26rpx;
		font-weight: 500;
		color: #333333;
		margin: auto;
		margin-top: 48rpx;
	}
	.btn-2 {
		width: 496rpx;
		height: 80rpx;
		background: #FFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		text-align: center;
		line-height: 80rpx;
		font-size: 26rpx;
		font-weight: 500;
		color: #333333;
		margin: auto;
		margin-top: 24rpx;
		border: 1rpx solid #F0F0F0;
	}
}


.serveBox {
				width: 690rpx;
				border-radius: 20rpx;

				.serve_tit {
					// margin-top: 30rpx;
					text-align: center;
					font-size: 36rpx;
					color: #333333;
					font-weight: bold;
					line-height: 40rpx;
				}
				
				.tc-img {
					width: 600rpx;
					margin: auto;
					margin-top:24rpx;
					image {
						width: 600rpx;
						height: 500rpx;
					}
				}
				
				.serve_cont {
					// margin: 0 30rpx;
					display: flex;
					// flex-wrap: wrap;
					// flex-direction: row;
					
					margin-top: 24rpx;
					overflow: hidden;
					overflow-x: auto;
					white-space: nowrap;

					.serve {
						margin: 30rpx 0;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						margin-right: 20rpx;
						padding: 16rpx 32rpx;
						// width: 190rpx;
						// height: 200rpx;
						box-sizing: border-box;
						border-radius: 20rpx;
						border: 1rpx solid #989898;


						.serve_num {
							margin-bottom: 20rpx;
							font-size: 32rpx;
							font-weight: bold;
							color: #333;
							line-height: 45rpx;
						}

						.serve_hour {
							font-size: 28rpx;
							font-weight: 400;
							color: #323232;
							line-height: 40rpx;
						}
					}

					.active {
						border: 2px solid #FF7A7A !important;
					}

				}
				
				.tc-btn-box {
					width: 560rpx;
					margin: auto;
					margin-top: 20rpx;
					display: flex;
					justify-content: space-between;
					.tc-btn1 {
						width: 220rpx;
						height: 80rpx;
						background-color: #FBBA45;
						border-radius: 10rpx;
						text-align: center;
						line-height: 80rpx;
					}
					.tc-btn2 {
						width: 220rpx;
						height: 80rpx;
						border-radius: 10rpx;
						text-align: center;
						line-height: 80rpx;
						background-color: #9A9A9A;
						color: #FFF;
					}
				}
			}
			
			
			.issue {
					width: 60rpx;
					height: 60rpx;
					background: linear-gradient(180deg, #FECB6F 0%, #FFAE1C 100%);
					border-radius: 0rpx 0rpx 0rpx 0rpx;
					opacity: 1;
					border-radius: 50%;
					font-size: 24rpx;
					font-weight: bold;
					color: #333333;
					padding: 20rpx;
					text-align: center;
					position: fixed;
					right: 32rpx;
					// bottom: 200rpx;
					bottom: 48%;
					z-index: 9;
				}
</style>
